/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectUnit;
import DTO.SavingBook_DTO;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Vector;

/**
 *
 * @author Nguyen Phi Hung
 */
public class SavingBook_BLL {
    
    ConnectUnit connect;
    
    public SavingBook_BLL(){
        this.connect = DAL.DAL.getDAL();
    }
    
    /**
     * get list of SavingBooks form database with Condition and OrderBy
     * @param Condition
     * @param OrderBy
     * @return
     * @throws Exception 
     */
    public Vector<SavingBook_DTO> getSavingBooks(String Condition, String OrderBy) throws Exception{
        //creater ResultSet to Select table SAVINGBOOK from database
        ResultSet result = this.connect.Select("SAVINGBOOK", Condition, OrderBy);
        //creater HashSet to save info of SavingBook
        Vector<SavingBook_DTO> savingbooks = new Vector<SavingBook_DTO>();
        while(result.next()){
            SavingBook_DTO sBook = new SavingBook_DTO();
            //get info of SavingBook from database
            sBook.setBookID(result.getString("BookID"));
            sBook.setCustomerID(result.getString("CustomerID"));
            sBook.setAccountBalance(BigInteger.valueOf(result.getLong("AccountBalance")));
            sBook.setInterestRate(result.getByte("InterestRate"));
            sBook.setInterestStartingDate(result.getDate("InterestStartingDate"));
            sBook.setOpeningDate(result.getDate("OpeningDate"));
            sBook.setSBStatus(result.getBoolean("SBStatus"));
            sBook.setTerm(result.getByte("Term"));
            sBook.setTypeID(result.getString("TypeID"));
            //add sBook into savingbooks
            savingbooks.add(sBook);
        }
        //return savingbooks
        return savingbooks;
    }
    
    /**
     * get list of SavingBooks form database with Condition
     * @param Condition
     * @return
     * @throws Exception 
     */
    public Vector<SavingBook_DTO> getSavingBooks(String Condition) throws Exception{
        return getSavingBooks(Condition, null);
    }
    
    /**
     * get list of SavingBooks form database
     * @return
     * @throws Exception 
     */
    public Vector<SavingBook_DTO> getSavingBooks() throws Exception{
        return getSavingBooks(null, null);
    }
    
    /**
     * get book by BookID from database
     * @param ID
     * @return
     * @throws Exception 
     */
    public SavingBook_DTO getSavingBookByID(String ID) throws Exception{
        Vector<SavingBook_DTO> sBooks = this.getSavingBooks("BookID = '" + ID+ "'");
        return (sBooks.size()>0) ? sBooks.toArray(new SavingBook_DTO[sBooks.size()])[0] : null;        
    }
    
    /**
     * 
     * @param list
     * @param index
     * @return 
     */
    public SavingBook_DTO getSavingBookByIndex(Vector<SavingBook_DTO> list, int index){
        return (index > list.size()) ? null : list.toArray(new SavingBook_DTO[list.size()])[index];
    }
    /**
     * Insert one info into database
     * @param info
     * @throws Exception 
     */
    public void Insert(SavingBook_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        Unity unity = new Unity();
        //info of saving book
        map.put("BookID", "");
        map.put("CustomerID", info.getCustomerID());
        map.put("TypeID", info.getTypeID());
        map.put("OpeningDate", unity.toString(info.getOpeningDate()));     
        map.put("AccountBalance","");
        
        String[] arr = {"BookID","CustomerID","TypeID","OpeningDate","AccountBalance"};
        //insert into database
        this.connect.Insert("SAVINGBOOK", map, arr);
    }
    
    /**
     * Update one info into database
     * @param info
     * @throws Exception 
     */
    public void Update(SavingBook_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        //info of saving book
        map.put("BookID", info.getBookID());
        map.put("CustomerID", info.getCustomerID());
        map.put("InterestRate", info.getInterestRate());
        map.put("OpeningDate", info.getOpeningDate());
        map.put("InterestStartingDate", info.getInterestStartingDate());
        map.put("AccountBalance", info.getAccountBalance());
        map.put("SBStatus", info.isSBStatus());
        //update info
        this.connect.Update("SAVINGBOOK", map, "BookID= "+ info.getBookID());
    }
    
    /**
     * Delete one info in database
     * @param info
     * @throws Exception 
     */
    public void Delete(SavingBook_DTO info) throws Exception{
        this.connect.Delete("SAVINGBOOK","BookID= "+ info.getBookID());
    }
}
